package com.woniu.exception.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.bean.R;
import com.woniu.exception.bean.TExhandle;
import com.woniu.exception.bean.TLiableside;
import com.woniu.exception.mapper.TLiablesideMapper;
import com.woniu.exception.service.ITExhandleService;
import com.woniu.exception.service.ITLiablesideService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-15
 */
@RestController
@RequestMapping("/tExhandle")
public class TExhandleController {

    @Autowired
    private ITExhandleService itExhandleService;

    @Autowired
    private ITLiablesideService itLiablesideService;

    @Autowired
    private TLiablesideMapper tLiablesideMapper;





    @CrossOrigin
    @GetMapping("type")
    public R findType(){

        System.out.println("这里是预加载查询类型");

        List<TLiableside> list = itLiablesideService.list();
        R r = new R();
        r=R.OK();
        Map map=new HashMap<>();
        map.put("records", list);
        r.setData(map);
        return r;
    }

    @ApiOperation(value = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "添加对象",value = "添加数据对象",example = "1")
    })
    @CrossOrigin
    @PostMapping
    public R add(@RequestBody TExhandle tExhandle) {
        /*public R add(@RequestBody TException tException) {*/

        TLiableside liableside = tLiablesideMapper.selectById(tExhandle.getLiablesideId());
        System.out.println("tLiableside = " + liableside);
        tExhandle.setLiableside(liableside);

        System.out.println("从前端拿到了tExhandle = " + tExhandle);
        boolean save = itExhandleService.save(tExhandle);

        R r=null;
        if(save){
            r=R.OK();
        } else {
            r=R.ERROR();
        }
        return r;
    }

    @CrossOrigin
    @PutMapping
    public  R updateCms(@RequestBody  TExhandle tExhandle) {

        System.out.println("修改方法，前端的数据传过来啦 " + tExhandle);

        QueryWrapper<TExhandle> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("track_Id",tExhandle.getTrackId());

        boolean update =  itExhandleService.update(tExhandle, queryWrapper);

        R r=null;
        if(update){
            r=R.OK();
        } else {
            r=R.ERROR();
        }
        return r;
    }

    @CrossOrigin
    @GetMapping("{trackId}")
    public TExhandle selectById(@PathVariable Integer trackId) {

        System.out.println("trackId = " + trackId);
        System.out.println("前端数据传进来了");

        QueryWrapper<TExhandle> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("track_Id",trackId);

        /* queryWrapper.ge();*/

        TExhandle tExhandle = itExhandleService.getOne(queryWrapper);

        return tExhandle;
    }





}
